﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Xml;

using Pibby.Core.Persistence;

namespace Pibby.Core.Feeds
{
	/// <summary>
	/// An individual episode or posting in an rss feed.
	/// </summary>
	public class Item : Identifiable
	{
		#region Xml Constants

		private const string ItemNode = "item";
		private const string TitleNode = "title";
		private const string GuidNode = "guid";
		private const string LinkNode = "link";
		private const string DescriptionNode = "description";
		private const string EnclosureNode = "enclosure";
		private const string PublicationDateNode = "pubdate";
		private const string ItunesDurationNode = "itunes:duration";
		#endregion

		#region Constructors

		public Item()
		{
		}

		/// <summary>
		/// Create a new Item from an xml fragment
		/// </summary>
		/// <param name="element"></param>
		/// <returns></returns>
		public static Item Create(XmlReader reader)
		{
			if (reader == null)
				throw new ArgumentNullException("reader", Resources.Item_NullObjectError);

			var item = new Item();

			bool foundGuid = false;

			// item or entry...
			if (reader.Name.Equals(ItemNode, StringComparison.CurrentCultureIgnoreCase))
			{
				bool complete = false;

				while (!complete && reader.Read())
				{
					string nodeName = reader.Name.ToLower();

					switch (reader.NodeType)
					{
						case XmlNodeType.Element:

							switch (nodeName)
							{
								case TitleNode:

									reader.Read();
									item.Title = reader.Value;
									break;

								case GuidNode:

									reader.Read();
									item.Id = reader.Value;
									foundGuid = true;
									break;

								case LinkNode:

									reader.Read();
									item.Link = reader.Value;
									break;

								case DescriptionNode:

									reader.Read();
									item.Description = reader.Value;
									break;

								case EnclosureNode:

									item.Enclosure = Enclosure.Create(reader);
									break;

								case PublicationDateNode:

									reader.Read();
									item.PublicationDate = new PublicationDate(reader.Value).Date;
									break;

								case ItunesDurationNode:

									reader.Read();
									item.Duration = reader.Value;
									break;
							}

							break;

						case XmlNodeType.EndElement:

							complete = nodeName.Equals(ItemNode, StringComparison.CurrentCultureIgnoreCase);
							break;
					}
				}
			}

			// no link ???
			if (item.Enclosure == null && !string.IsNullOrEmpty(item.Description))
			{
				var uriList = LinkScraper.Find(item.Description);

				foreach (Uri link in uriList)
				{
					if (link.AbsoluteUri.EndsWith(FileConstants.Mp3FileExtension))
					{
						item.Enclosure = Enclosure.Create(link.AbsoluteUri);
					}
				}
			}

			if (item.Enclosure != null && !foundGuid)
			{
				item.Id = item.Enclosure.Url.AbsoluteUri;
			}

			return item;
		}

		#endregion

		public override string ToString()
		{
			return string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0}", this.Title);
		}

		#region Properties

		public string Title
		{
			get;
			set;
		}
				
		public string Link
		{
			get;
			set;
		}

		public string Description
		{
			get;
			set;
		}

		public DateTime PublicationDate
		{
			get;
			set;
		}

		public Enclosure Enclosure
		{
			get;
			set;
		}

		public string Duration
		{
			get;
			set;
		}

		#endregion
	}


}
